package com.gene.controller;

import com.gene.pojo.UpmsAddress;
import com.gene.service.IUpmsAddressService;
import com.gene.utils.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 地址管理的控制类
 */
@Controller
@RequestMapping("/gene-safe/upmsAddress")
public class UpmsAddressController {
    @Autowired
    private IUpmsAddressService upmsAddressService;

    /**
     * 查询    userId为查询条件
     * @param upmsAddress
     * @return
     */
    @RequestMapping(value="/query")
    @ResponseBody
    public ResultData query(UpmsAddress upmsAddress){
        ResultData result=new ResultData();
        List<UpmsAddress> list=upmsAddressService.query(upmsAddress);

        if(list.size()>0){
            result.setData(list);
            result.setStatus("1");
            result.setMsg("查询成功！");
        }else{
            result.setStatus("0");
            result.setMsg("没有数据！");
        }

        return result;
    }



    /**
     * 查询默认地址  userId  isdefault=1为查询条件
     * @param upmsAddress
     * @return
     */
    @RequestMapping(value="/selectDefau")
    @ResponseBody
    public ResultData selectDefau(UpmsAddress upmsAddress){
        ResultData result=new ResultData();
        upmsAddress.setIsdefault((byte) 1);
        UpmsAddress u=upmsAddressService.selectDefau(upmsAddress);

        if(null != u){
            result.setData(u);
            result.setStatus("1");
            result.setMsg("查询成功！");
        }else{
            result.setStatus("0");
            result.setMsg("没有数据！");
        }

        return result;
    }




    /**
     * 查询    AddressId为查询条件
     * @param upmsAddress
     * @return
     */
    @RequestMapping(value="/selectId")
    @ResponseBody
    public ResultData selectId(UpmsAddress upmsAddress){
        ResultData result=new ResultData();
        UpmsAddress endity=upmsAddressService.selectId(upmsAddress);

        if(endity !=null){
            result.setData(endity);
            result.setStatus("1");
            result.setMsg("查询成功！");
        }else{
            result.setStatus("0");
            result.setMsg("没有数据！");
        }

        return result;
    }

    /**
     *  收货地址  设置为默认地址的方法
     */
    @RequestMapping(value="/defau")
    @ResponseBody
    public ResultData defau(UpmsAddress upmsAddress){
        ResultData result=new ResultData();
        upmsAddress.setIsdefault((byte) 1);
        boolean  a= upmsAddressService.defau(upmsAddress);
        if(a){
            result.setStatus("1");
            result.setMsg("修改成功...");
        }else{
            result.setStatus("0");
            result.setMsg("修改失败！");
        }
        return result;
    }



    /**
     * 新增
     * @param upmsAddress
     * @return
     */
    @RequestMapping(value="/add")
    @ResponseBody
    public ResultData add(UpmsAddress upmsAddress){
        ResultData result =new ResultData();
        boolean a=upmsAddressService.add(upmsAddress);

        if(a){
            result.setStatus("1");
            result.setMsg("新增成功...");
        }else{
            result.setStatus("0");
            result.setMsg("新增失败！");
        }
        return result;
    }

    /**
     * 修改
     *
     * @param upmsAddress
     * @return
     */
    @RequestMapping(value="/edit")
    @ResponseBody
    public ResultData edit(UpmsAddress upmsAddress){
        ResultData result =new ResultData();
        boolean a=upmsAddressService.edit(upmsAddress);
        if(a){
            result.setStatus("1");
            result.setMsg("修改成功...");
        }else{
            result.setStatus("0");
            result.setMsg("修改失败！");
        }
        return result;
    }

    /**
     * 删除
     * @param upmsAddress
     * @return
     */
    @RequestMapping(value="/delete")
    @ResponseBody
    public ResultData delete(UpmsAddress upmsAddress) {
        ResultData result = new ResultData();
        boolean a=upmsAddressService.delete(upmsAddress);
        if(a){
            result.setStatus("1");
            result.setMsg("删除成功...");
        }else{
            result.setStatus("0");
            result.setMsg("删除失败！");
        }
        return result;
    }


}
